#include "IRTX.h"
#include "PWM.h"
#include "Function.h"
#if _DEBUG_EN_
#include "USART.h"
#endif          

const uint8 TAB_BIT_IRTX[] = {0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
uint8 IR_TxBuf[C_SIZEOF_IRTX_BUF]={0xAA,0x01,0x20,0x02,0x00,0x18,0x0D,0x00,0x0B,0x02,0x0D,0x00,0x00};
volatile Typdef_IRTX_CTRL    IRTX_Ctrl={0};

void initIRTX(void)
{
#if _DEBUG_EN_
    // initUART();
    initUSART();
#else
    initPWM();
    PWM_SetPeriod(PWM_CHN2,MAX_PWM_DUTY);
    PWM2_SET_DUTY(140);
#endif
}

void IR_Send(uint8 key)
{
    if(key != KF_CMD_NULL)
    {
        uint8 i;
        uint8 cs = 0;

        // WAKEUP_BLE();

        IR_TxBuf[4] = KeyPressTimes;
        IR_TxBuf[10] = key;

        for(i=0; i<C_SIZEOF_IRTX_BUF-1; i++)
        {
            cs += IR_TxBuf[i];
        }
        IR_TxBuf[C_SIZEOF_IRTX_BUF-1] = cs;

    #if _DEBUG_EN_
        for(i=0; i<C_SIZEOF_IRTX_BUF; i++)
        {
            USART_TX_DATA(IR_TxBuf[i]);
            // UART_SendByte(IR_TxBuf[i]);
            // UART_SendByte(0x55);
        }
    #else
        IRTX_Ctrl.Cnt = 0;
        IRTX_Ctrl.SM = 0;
        IRTX_Ctrl.TxBitCount = 0;
        IRTX_Ctrl.F_IR_Send_IRQ = YES;
    #endif  
    }
}

void IRTX_Isr(void)
{
    if(IRTX_Ctrl.F_IR_Send_IRQ)
    {
        if(IRTX_Ctrl.Cnt)   
        {
            IRTX_Ctrl.Cnt--;
        }

        if(IRTX_Ctrl.Cnt == 0)
        {
            switch(IRTX_Ctrl.SM)
            {
                case IRT_HEAD_H:
                    EN_PWM2();
                    IRTX_Ctrl.Cnt = C_TM_IRTX_HEAD_H;
                    IRTX_Ctrl.SM++;
                break;

                case IRT_HEAD_L:
                    DIS_PWM2();
                    IRTX_Ctrl.Cnt = C_TM_IRTX_HEAD_L;
                    IRTX_Ctrl.SM++;
                break;

                case IRT_SYNC:
                    EN_PWM2();
                    IRTX_Ctrl.Cnt = C_TM_IRTX_SYNC;
                    IRTX_Ctrl.SM++;
                break;

                case IRT_DATA:
                    DIS_PWM2();
                    if(IR_TxBuf[IRTX_Ctrl.TxBitCount>>3] & TAB_BIT_IRTX[IRTX_Ctrl.TxBitCount&0x07])     
                    {
                        IRTX_Ctrl.Cnt = C_TM_IRTX_DATA1;
                    }
                    else
                    {
                        IRTX_Ctrl.Cnt = C_TM_IRTX_DATA0;
                    }
                    IRTX_Ctrl.TxBitCount++;
                    if(IRTX_Ctrl.TxBitCount >= C_MAX_IRTX_BITCOUNT)
                    {
                        IRTX_Ctrl.TxBitCount = 0;
                        IRTX_Ctrl.SM++;
                    }
                    else
                    {
                        IRTX_Ctrl.SM--;
                    }
                break;

                case IRT_SYNC_STOP:
                    EN_PWM2();
                    IRTX_Ctrl.Cnt = C_TM_IRTX_SYNC;
                    IRTX_Ctrl.SM++;
                break;

                case IRT_STOP_L:
                    DIS_PWM2();
                    IRTX_Ctrl.Cnt = C_TM_IRTX_STOP_L;
                    IRTX_Ctrl.SM++;
                break;

                case IRT_STOP_H:
                    EN_PWM2();
                    IRTX_Ctrl.Cnt = C_TM_IRTX_STOP_H;
                    IRTX_Ctrl.SM++;
                break;

                case IRT_END:
                default:
                    DIS_PWM2();
                    IRTX_Ctrl.SM = 0;
                    IRTX_Ctrl.F_IR_Send_IRQ = NO;
                break;
            }

        }

    }
}
